package com.wh.service.impl;

import com.wh.beans.ConstVar;
import com.wh.beans.SysMenuModel;
import com.wh.dao.BaseDao;
import com.wh.dao.ext.SysMenuDao;
import com.wh.model.BaseModel;
import com.wh.model.SysMenu;
import com.wh.service.IExt.ISysEnumService;
import com.wh.service.IExt.ISysMenuService;
import com.legend.utils.ReturnResult;
import com.legend.utils.SessionUtils;
import com.legend.utils.Status;
import com.legend.utils.i18nUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class SysMenuService extends BaseServiceImp implements ISysMenuService {
    private SysMenuService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static ISysMenuService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static ISysMenuService getInstance() {
        if (instance == null) {
            synchronized (ISysMenuService.class) {
                if (instance == null) {
                    instance = new SysMenuService();
                }
            }
        }
        return instance;
    }
    public ReturnResult delById(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        return delByMap(map);
    }

    @Override
    public ReturnResult delByMap(Map<String, Object> map) {
        SqlSession session = getSqlSession();
        SysMenuDao dao = session.getMapper(SysMenuDao.class);
        int effect = dao.delByMap(map);
        ReturnResult result = new ReturnResult();
        if (effect > 0) {
            result.setStatus(Status.SUCCESS);
            result.setMsg(i18nUtils.getVal("100001000"));
        }
        session.close();
        return result;
    }

    @Override
    public ReturnResult editByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysMenuDao dao = session.getMapper(SysMenuDao.class);
        ReturnResult result = editByModel(dao,model);
        session.close();
        return result;
    }


    @Override
    public ReturnResult insertByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysMenuDao dao = session.getMapper(SysMenuDao.class);
        SysMenu menu = (SysMenu) model;
        if (menu.getParentId() == null) {
            menu.setParentId("0");
        }
        ReturnResult result= insertByModel(dao,menu);
        session.close();
        return result;
    }

    /**
     * 审核
     * @param state
     * @param idArr
     * @return
     */
    public ReturnResult audit(int state,String[] idArr) {
        SqlSession session = getSqlSession();
        SysMenuDao dao = session.getMapper(SysMenuDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("idArr", idArr);
        map.put("state", state);
        Integer num = dao.audit(map);
        ReturnResult result = new ReturnResult();
        if (num > 0) {
            result.setMsg(i18nUtils.getVal("100001000"));
                    result.setStatus(Status.SUCCESS);
        }
        session.close();
        return result;
    }

    /**
     * 根据ID获取一条数据
     *
     * @param id
     * @return
     */
    public ReturnResult selectById(String id) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.ERROR);
        BaseDao dao = session.getMapper(SysMenuDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        List<SysMenu> lst = dao.selectByMap(map);
        if (lst.size() == 1) {
            result.setData(lst.get(0));
            result.setStatus(Status.SUCCESS);
        } else {
            result.setMsg(i18nUtils.getVal("100004000"));
        }
        session.close();
        return result;
    }

    /**
     * 获得菜单列表
     *
     * @param defaultOption 是否包含默认项
     * @param next          是否获取下一级
     * @param status        需要获取的状态
     * @return
     */
    public ReturnResult getMenuList(boolean defaultOption, boolean next, int status) {
        List<SysMenuModel> lst = GetMenuByPid("0", next, defaultOption, status);
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.SUCCESS);
        result.setRows(lst);
        return result;
    }

    /**
     * @param pid 父类ID
     * @return 返回重组后的所有状态的List<SysMenuModel>
     * @para next是否获取下级栏目
     */
    public List<SysMenuModel> GetMenuByPid(String pid, boolean next) {
        return GetMenuByPid(pid, next, false, -1);
    }

    /**
     * @param pid 父类ID
     * @return 返回重组后的List<SysMenuModel>
     * @para next是否获取下级栏目
     * @defaultOption 是否包含默认项
     */
    public List<SysMenuModel> GetMenuByPid(String pid, boolean next, boolean defaultOption, int status) {
        List<SysMenuModel> Lst = getMenuFromCache(pid, status);
        List<SysMenuModel> tmpLst = new ArrayList<SysMenuModel>();
        if (next) {
            tmpLst = Lst;
        } else {
            for (SysMenuModel sysMenuModel : Lst) {
                if (sysMenuModel.getPid().equals(pid)) {
                    tmpLst.add(sysMenuModel);
                }
            }
        }
        if (defaultOption) {
            SysMenuModel menu = new SysMenuModel();
            menu.setId("0");
            menu.setText("根目录");
            tmpLst.add(0, menu);
        }
        return tmpLst;
    }

    /**
     * 从缓存中获取菜单列表，如果无缓存重建缓存数据
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<SysMenuModel> getMenuFromCache(String pid, int status) {
        List<SysMenuModel> tmpLst;
        SqlSession session = getSqlSession();
        SysMenuDao dao = session.getMapper(SysMenuDao.class);
        Object menuLst = SessionUtils.getInstance().get(ConstVar.SYSUSERMENU_SESSIONKEY);
        List<Integer> mList = new ArrayList<Integer>();
        List<SysMenu> lst;
        Map<String, Object> map = new HashMap<String, Object>();
        if (status > -1) {
            map.put("status", status);
        }
        if (menuLst != null) {
            mList = (List<Integer>) menuLst;
            map.put("idLst", mList);
            lst = dao.selectIn(map);
        } else {
            lst = dao.selectByMap(map);
        }

        tmpLst = GetMenuList(pid, lst, true);
        session.close();
        return tmpLst;
    }

    /**
     * 获取栏目列表
     */
    private List<SysMenuModel> GetMenuList(String pid, List<SysMenu> lst, boolean next) {
        List<SysMenuModel> tmpLst = new ArrayList<SysMenuModel>();

        for (SysMenu sysMenu : lst) {
            if (sysMenu.getParentId().equals(pid)) {
                SysMenuModel sysMenuModel = new SysMenuModel();
                sysMenuModel.setId(sysMenu.getId());
                sysMenuModel.setText(sysMenu.getMenuTitle());
                sysMenuModel.setPid(sysMenu.getParentId());
                sysMenuModel.setAttributes("{\"url\":\"" + sysMenu.getMenuUrl() + "\"}");
                sysMenuModel.setIconCls(sysMenu.getMenuIconcls());
                sysMenuModel.setSort(sysMenu.getSort());
                sysMenuModel.setStatus(sysMenu.getStatus());
                sysMenuModel.setUpdateDate(sysMenu.getUpdateDate());
                if (next)
                    sysMenuModel.setChildren(GetMenuList(sysMenu.getId(), lst, next));
                tmpLst.add(sysMenuModel);
            }
        }
        return tmpLst;
    }


}